package lib;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import dao.DictionaryDAO;
import pojo.Dictionary;

public class DictionaryTableModel extends AbstractTableModel{
	public static final int DICTIONARYID_INDEX = 0;
	public static final int TOPICID_INDEX = 1;
	public static final int FILTERKEYWORDS_INDEX = 2;
	public static final int FILTEROPTIONALKEYWORDS_INDEX = 3;
	protected String[] columnNames = {"DictionaryID", "TopicID", "FilterKeywords", "FilterOptionalKeywords"};
	protected Vector<Dictionary> dataVector;
	public DictionaryTableModel(){
		dataVector = new Vector<Dictionary>();
		DictionaryDAO dictionarydao = new DictionaryDAO();
		List<Dictionary> dictionary = dictionarydao.getList();
		dataVector.addAll(dictionary);
	}
	public String getColumnName(int column){
		return columnNames[column];
	}
	public Class getColumnClass(int column){
		switch(column){
		case DICTIONARYID_INDEX:
			return Integer.class;
		case TOPICID_INDEX:
			return Integer.class;
		case FILTERKEYWORDS_INDEX:		
			return String.class;
		case FILTEROPTIONALKEYWORDS_INDEX:
			return String.class;
		default:
			return Object.class;
		}
	}
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return dataVector.size();
	}
	@Override
	public Object getValueAt(int row, int column) {
		// TODO Auto-generated method stub
		Dictionary dictionary = dataVector.get(row);
		switch(column){
		case DICTIONARYID_INDEX:
			return dictionary.getDictionaryId();
		case TOPICID_INDEX:
			return dictionary.getTopicId();
		case FILTERKEYWORDS_INDEX:
			return dictionary.getFilterKeywords();
		case FILTEROPTIONALKEYWORDS_INDEX:
			return dictionary.getFilterOptionalKeywords();
		default:
			return dictionary;	
		}
	}
	public void setValueAt(Object value, int row, int column){
		Dictionary dictionary = dataVector.get(row);
		switch(column){
		case DICTIONARYID_INDEX:
			dictionary.setDictionaryId((Integer)value);
			break;
		case TOPICID_INDEX:
			dictionary.setTopicId((Integer)value);
			break;
		case FILTERKEYWORDS_INDEX:
			dictionary.setFilterKeywords((String)value);
			break;
		case FILTEROPTIONALKEYWORDS_INDEX:
			dictionary.setFilterOptionalKeywords((String)value);
			break;
		default:
			break;
		}
		fireTableCellUpdated(row, column);
	}
	public boolean isCellEditable(int row, int column){
		if(column == DICTIONARYID_INDEX || column == TOPICID_INDEX)
			return false;
		return true;
	}
	public boolean hasEmptyRow(){
		if(dataVector.size() == 0)
			return true;
		return false;
	}
	public void addEmptyRow(){
		dataVector.add(new Dictionary());
	}
	public void saveToDatabase(){
		DictionaryDAO dictionarydao = new DictionaryDAO();
		for(int i= 0 ; i< dataVector.size();i++)
		{
			dictionarydao.UpdateOrInsert(dataVector.get(i));
		}
		fireTableDataChanged();
	}
	public void recoveryData(){
		DictionaryDAO dictionarydao = new DictionaryDAO();
		List<Dictionary> dictionary = dictionarydao.getList();
		dataVector.clear();
		dataVector.addAll(dictionary);
		fireTableDataChanged();
	}
}
